package com.egao.common.component.scheduled;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egao.common.module.company.entity.Recruit;
import com.egao.common.module.company.service.RecruitService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author lym
 * @date 2024/6/28 10:20
 */
@ApiModel(description = "招聘信息定时任务")
@Component
public class RecruitScheduled {

    @Resource
    private RecruitService recruitService;

    @Scheduled(cron = "0 59 23 ? * *")
    @ApiOperation("每天23点59分更改过期信息状态")
    public void overdue() {
        Date today = new Date();
        Date beginToday = DateUtil.beginOfDay(today);
        Date endToday = DateUtil.endOfDay(today);

        List<Recruit> list = recruitService.list(new LambdaQueryWrapper<Recruit>()
                .select(Recruit::getId)
                .eq(Recruit::getOverdue, "0")
                .ge(Recruit::getValidTime, beginToday)
                .le(Recruit::getValidTime, endToday)
        );

        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(d -> {
                d.setOverdue("1");
            });

            recruitService.updateBatchById(list);
        }
    }
}
